#pragma once

extern "C"
{
#define __STDC_FORMAT_MACROS
#define __STDC_CONSTANT_MACROS //for UINT64_C
#include "inttypes.h"
#include "libavformat/avformat.h"
}

namespace FFMPEGCPP
{
	bool	InitFormat();
	void	UnInitFormat();

	typedef struct _PacketQueue 
	{ 
		AVPacketList*	first;
		AVPacketList*	last; 
		int nb_packets; 
		int size; 
	} PacketQueue; 

	void packetque_init( PacketQueue* p );
	bool packetque_pushback( PacketQueue* p,AVPacket* packet );
	bool packetque_popfront( PacketQueue* p,AVPacket* packet );


	class CAVDecodeBase;
	class CAVInputFile
	{
	public:
		CAVInputFile();
		~CAVInputFile();

		bool	Open( const char* filename,const char* formatname = 0 );
		void	Close();
		bool	IsValid(){ return 0!= m_pFormatCtx;};

		bool	ReadPacket( AVPacket* pPacket );
		void	FreePacket( AVPacket* pPacket );
		bool	ProcessPacket();

		AVFormatContext*	GetContext();
		unsigned int		GetStreamCount();
		enum AVMediaType	GetStreamMediaType( unsigned int nStreamIndex );
		bool				SetDecodeForStream( unsigned int nStreamIndex,CAVDecodeBase* decoder );
		CAVDecodeBase*		GetDecode(unsigned int nStreamIndex );

		int64_t				GetTotalTime();//seconds 

	private:
		AVInputFormat*		m_pAVInputFormat;
		AVFormatContext*	m_pFormatCtx;
		CAVDecodeBase**		m_pDecodeList;
		int64_t				m_Duration;
	};
	
};
